mod commandargs;

use std::ops::Deref;
use std::process::Command;
use clap::builder::Str;
use clap::Parser;
// use rs_docker::Docker;
use crate::commandargs::{run, subcommand_exec, subcommand_run};

///sub process interface
#[derive(Parser, Clone, Debug)]
struct Arg {
    ///image name
    #[arg(long, short,default_value = "instantbox/ubuntu:18.04")]
    image: String,

    #[command(subcommand)]
    subcommand: DO,

}

#[derive(Parser, Clone, Debug)]
enum DO {
    ///run image
    RUN,

    ///search image local
    FIND,

    ///search image remote
    SEARCH,

    ///pull image from remote
    PULL,

    ///Executes a command with arguments
    exec(Mutiple),
}

#[derive(Debug,Parser,Clone)]
struct Mutiple{
    ///Executes a command with arguments exec COMMAND
    exec:Vec<String>,

}
fn main() {
    // let mut docker = match Docker::connect("unix:///var/run/docker.sock") {
    //     Ok(docker) => docker,
    //     Err(e) => { panic!("{}", e); }
    // };
    //
    //
    //
    // let _result = docker.create_image("instantbox/ubuntu".to_owned(), "18.04".to_owned());
    //
    // //docker run <image>:<tag>
    // //
    // //ttyd_linux.x86_64" -p 1588 bash
    // let mut command = Command::new("docker");
    // command.args(["run", "-d", "-p","1589:1589","instantbox/ubuntu:18.04","ttyd_linux.x86_64","-p","1589","bash"]);
    // let output = command.output().unwrap();
    // let cow_stdout = String::from_utf8_lossy(&output.stdout);
    // let cow_sterr = String::from_utf8_lossy(&output.stderr);
    // println!("{} \nplease use port 1589 with web", cow_stdout);
    // println!("{}", cow_sterr);

    let args = Arg::parse();
    let img:String = args.image;
    let command = "docker".to_string();


    let find_array = ["images"];
    let search_array = ["search", img.deref()];
    let pull_array = ["pull",img.deref()];
    match args.subcommand {
        DO::RUN => {
            subcommand_run(command,img)
        }
        DO::FIND => {run(command,&find_array)}
        DO::SEARCH => {run(command,&search_array)}
        DO::PULL =>{run(command,&pull_array)}
        DO::exec(mutiple) =>{
            subcommand_exec(command,mutiple);
        }
    }

}